﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Web.UI.WebControls;
using Resources;
using Wicresoft.RDW.Presentation;
using Wicresoft.RDW.Utility;
using System.Collections.Generic;

namespace Wicresoft.RDW.LabManage
{
    public partial class View : BasePage_BackEnd
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            pager.PageChange += new Wicresoft.RDW.UserControls.PagerControl.PageRefresh(PagerControll_PageChange);
            if (!IsPostBack)
            {
                pager.CurrentPageIndex = 0;
                PagerControll_PageChange(pager.CurrentPageIndex);
            }
        }
        protected void PagerControll_PageChange(int pageIndex)
        {
            var dataPagerSource = LabCloseSchedulePresent.GetPaged(pageIndex, pager.PageSize, GetLambaCondition(), p => p.StartDate, true);
            this.LabCloseScheduleGW.DataSource = dataPagerSource;
            this.LabCloseScheduleGW.DataBind();
            var count = 0;
            count = dataPagerSource.TotalCount;
            pager.IntialProperties(count);
        }
        private Expression<Func<Model.LabCloseSchedule, bool>> GetLambaCondition()
        {
            Expression<Func<Model.LabCloseSchedule, bool>> searchExp = p => true;
            if (!string.IsNullOrEmpty(ucLocationLab.LocationValue))
            {
                int locationid = int.Parse(ucLocationLab.LocationValue);
                var labids = LabPresent.GetList(g => g.LocationId == locationid).Select(g => g.LabId);
                searchExp = searchExp.AndPredicate<Model.LabCloseSchedule>(p => labids.Contains(p.LabId));
                if (!string.IsNullOrEmpty(ucLocationLab.LabValue))
                {
                    int labid = int.Parse(ucLocationLab.LabValue);
                    searchExp = searchExp.AndPredicate<Model.LabCloseSchedule>(p => p.LabId == labid);
                }
            }
            return searchExp;
        }
        public override void InitControlResource()
        {
            btnCloseLab.Text = ManageResource.mgr_btnCloseLab;
            btnQuery.Text = ManageResource.mgr_btnQuery;
            int i = 0;
            LabCloseScheduleGW.Columns[i++].HeaderText = NameResource.No;
            LabCloseScheduleGW.Columns[i++].HeaderText = ManageResource.mgr_lblLocationName;
            LabCloseScheduleGW.Columns[i++].HeaderText = ManageResource.mgr_lblLabName;
            LabCloseScheduleGW.Columns[i++].HeaderText = ManageResource.mgr_lblStartTime;
            LabCloseScheduleGW.Columns[i++].HeaderText = ManageResource.mgr_lblEndTime;
            LabCloseScheduleGW.Columns[i++].HeaderText = ManageResource.mgr_lblCloseReason;
            LabCloseScheduleGW.Columns[i++].HeaderText = ManageResource.mgr_lblAction;
        }

        protected void btnCloseLab_Click(object sender, EventArgs e)
        {
            Response.Redirect("/LabManage/Update.aspx?pagestatus=new");
        }
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            phOpen.Visible = true;
            pager.CurrentPageIndex = 0;
            PagerControll_PageChange(pager.CurrentPageIndex);
        }
        protected void LabCloseScheduleGW_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            Model.LabCloseSchedule labCloseSchedule = e.Row.DataItem as Model.LabCloseSchedule;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                (e.Row.Cells[0].FindControl("lblNum") as Label).Text = (e.Row.RowIndex + 1 + (pager.CurrentPageIndex) * pager.PageSize).ToString();
                var lbtnDelete = e.Row.Cells[5].FindControl("lbtnDelete") as LinkButton;
                var lbtnUpdate = e.Row.Cells[5].FindControl("lbtnUpdate") as LinkButton;
                lbtnDelete.Text = ManageResource.mgr_btnDelete;
                lbtnUpdate.Text = ManageResource.mgr_btnEdit;
                lbtnDelete.Attributes.Add("onclick", "return confirm('" + NameResource.Delete_Confirm + "');");
                Model.Lab lab = LabPresent.GetById((int)labCloseSchedule.LabId);
                if (lab != null)
                {
                    (e.Row.Cells[1].FindControl("lblLabName") as Label).Text = lab.Name;
                    Model.Location location = LocationPresent.GetById((int)lab.LocationId);
                    if (location != null)
                    {
                        (e.Row.Cells[0].FindControl("lblLocationName") as Label).Text = Language == Language.CN ? location.Name_CN : location.Name_EN;
                    }
                }
                if (labCloseSchedule.ReasonId != 0)
                {
                    Model.Reason reason = ReasonPresent.GetById((int)labCloseSchedule.ReasonId);
                    if (reason != null)
                    {
                        (e.Row.Cells[3].FindControl("lblReason") as Label).Text = Language == Language.CN ? reason.Desc_CN : reason.Desc_EN;
                    }
                }
                else
                {
                    (e.Row.Cells[3].FindControl("lblReason") as Label).Text = labCloseSchedule.OtherReason;
                }
                if (labCloseSchedule.StartDate < DateTime.Today)
                {
                    lbtnDelete.Attributes.Add("disabled", "disabled");
                    lbtnDelete.Attributes.Remove("onclick");
                    lbtnDelete.Attributes.Add("onclick", "return false;");
                }
                if (labCloseSchedule.EndDate < DateTime.Today)
                {
                    lbtnUpdate.Attributes.Add("disabled", "disabled");
                    lbtnUpdate.Attributes.Remove("onclick");
                    lbtnUpdate.Attributes.Add("onclick", "return false;");
                }
            }
        }

        protected void LabCloseScheduleGW_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int i = e.RowIndex;
            int scheduleId = (int)this.LabCloseScheduleGW.DataKeys[i]["ScheduleId"];
            LabCloseSchedulePresent.Delete(scheduleId);
            phOpen.Visible = true;
            pager.CurrentPageIndex = 0;
            PagerControll_PageChange(pager.CurrentPageIndex);
        }

        protected void LabCloseScheduleGW_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int i = e.RowIndex;
            int scheduleId = (int)this.LabCloseScheduleGW.DataKeys[i]["ScheduleId"];
            Response.Redirect("/LabManage/Update.aspx?pagestatus=edit&id=" + scheduleId);
        }
    }
}